package com.googlecode.imanager.client.action;

import java.io.Serializable;

import com.googlecode.imanager.client.Control;
import com.googlecode.imanager.client.exception.ActionFailureException;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class Action implements Serializable {

    private static final long serialVersionUID = 1L;

    // TODO fazer esquema de os controles herdarem 
    //      essas propriedades da action por default
    //      se estiverem preenchidas
    private String text;
    private String hint;
    private Boolean visible;
    private Boolean disabled;
    private Character accessKey;

    public Action() {
    }

    public Action(String text) {
        this.text = text;
    }
    
    protected String getText() {
        return text;
    }

    protected void setText(String text) {
        this.text = text;
    }

    protected String getHint() {
        return hint;
    }

    protected void setHint(String hint) {
        this.hint = hint;
    }

    protected Character getAccessKey() {
        return accessKey;
    }

    protected void setAccessKey(Character accessKey) {
        this.accessKey = accessKey;
    }

    public boolean isDisabled() {
        return disabled == null ? false : disabled;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public boolean isVisible() {
        return visible == null ? false : visible;
    }

    public Boolean getVisible() {
        return visible;
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }

    public void execute(Control sender) throws ActionFailureException, IllegalArgumentException {
        if (sender == null){
            throw new IllegalArgumentException("no sender specified");
        }
        try {
            doExecute(sender);            
        } catch (Exception e) {
            throw new ActionFailureException(this, e);
        }
    }
    
    protected abstract void doExecute(Control sender) throws Exception;
    
}
